package com.atguigu.edu.controller;


import com.atguigu.commonutils.R;
import com.atguigu.edu.entity.Teacher;
import com.atguigu.edu.entity.vo.TeacherQuery;
import com.atguigu.edu.service.TeacherService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author 一个平凡de人
 * @since 2022-03-02
 */
@RestController
@Api(description="讲师管理")
@CrossOrigin //解决跨域
@RequestMapping("/edu/teacher")
public class TeacherController {
    @Autowired
    private TeacherService teacherService;

    @ApiOperation(value = "查询所有讲师数据")
    @GetMapping("/findAll")
    public R list(){
        List<Teacher> list = teacherService.list(null);
        return R.ok().data("items", list);
    }

    @ApiOperation(value = "根据ID删除讲师")
    @DeleteMapping("/delete/{id}")
    public R removeById(
            @ApiParam(name = "id", value = "讲师ID", required = true)
            @PathVariable String id){
        teacherService.removeById(id);
        return R.ok();
    }

    @ApiOperation(value = "分页讲师列表")
    @GetMapping("/limit/{page}/{limit}")
    public R pageList(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,

            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit){

        Page<Teacher> pageParam = new Page<>(page, limit);

        // 只使用分页参数iPage，未使用查询参数wrapper
        teacherService.page(pageParam, null);
        List<Teacher> records = pageParam.getRecords();
        long total = pageParam.getTotal();

        return  R.ok().data("total", total).data("rows", records);
    }

    @ApiOperation(value = "分页查询讲师列表")
    @GetMapping("/query/{page}/{limit}")
    public R pageQuery(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,

            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,
            // required = false 表示参数可以为空
            @ApiParam(name = "teacherQuery", value = "查询对象", required = false)
                    TeacherQuery teacherQuery){

        Page<Teacher> pageParam = new Page<>(page, limit);

        // 使用分页参数iPage和查询参数wrapper【多条件组合查询带分页】
        teacherService.pageQuery(pageParam, teacherQuery);
        List<Teacher> records = pageParam.getRecords();
        long total = pageParam.getTotal();
        System.out.println(records);
        return  R.ok().data("total", total).data("rows", records);
    }

    /**
     * 自定义
     * */
    @ApiOperation(value = "分页查询讲师列表")
    @PostMapping("/query/{page}/{limit}")
    public R pageQueryDiy(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,

            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,

            @RequestBody(required=false) TeacherQuery teacherQuery){

        // 创建Page对象
        Page<Teacher> pageParam = new Page<>(page, limit);

        // 使用分页参数iPage和查询参数wrapper【多条件组合查询带分页】
        teacherService.pageQuery(pageParam, teacherQuery);
        List<Teacher> records = pageParam.getRecords();
        long total = pageParam.getTotal();

        return  R.ok().data("total", total).data("rows", records);
    }

    @ApiOperation(value = "新增讲师")
    @PostMapping("/add")
    public R add(
            @ApiParam(name = "teacher", value = "讲师对象", required = true)
            @RequestBody Teacher teacher){

        boolean save = teacherService.save(teacher);
        if(save) {
            return R.ok();
        } else {
            return R.error();
        }
    }


    @ApiOperation(value = "根据ID查询讲师")
    @GetMapping("/queryById/{id}")
    public R queryById(
            @ApiParam(name = "id", value = "讲师ID", required = true)
            @PathVariable String id){

        Teacher teacher =  teacherService.getById(id);
        return R.ok().data("teacher",teacher);
    }

    @ApiOperation(value = "修改讲师")
    @PostMapping("/update")
    public R update(
            @ApiParam(name = "teacher", value = "讲师对象(id参数必须填写)", required = true)
            @RequestBody Teacher teacher){
        boolean flag = teacherService.updateById(teacher);
        if(flag){
            return R.ok();
        }else{
            return R.error();
        }
    }

    @ApiOperation(value = "根据ID修改讲师")
    @PutMapping("/updateById/{id}")
    public R updateById(
            @ApiParam(name = "id", value = "讲师ID", required = true)
            @PathVariable String id,

            @ApiParam(name = "teacher", value = "讲师对象", required = true)
            @RequestBody Teacher teacher){

        teacher.setId(id);
        teacherService.updateById(teacher);
        return R.ok();
    }
}